package com.translate.web.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import com.common.util.ListConvertMap;
import com.translate.web.constant.RedisConstant;
import com.translate.web.dao.CustomerAccessLabelMapper;
import com.translate.entity.CustomerAccessLabel;
import com.translate.web.param.contract.ICustomerAccessLabelParam;
import com.translate.web.param.contract.ICustomerAccessLabelUpdateParam;
import com.translate.web.service.ICustomerAccessLabelService;
import com.basic.service.impl.BaseServiceImpl;
import com.translate.vo.contract.ICustomerFlagVO;

@Service
public class CustomerAccessLabelServiceImpl extends BaseServiceImpl<CustomerAccessLabel>
		implements ICustomerAccessLabelService {

	public CustomerAccessLabelServiceImpl(CustomerAccessLabelMapper mapper) {
		super(mapper);
		// TODO Auto-generated constructor stub
	}

	@Override
	public <S extends ICustomerAccessLabelParam> int parseBatchInsert(List<S> accessLabelParams, int customerId) {

		if (null == accessLabelParams) {
			return 1;
		}

		for (S s : accessLabelParams) {
			s.setCustomerId(customerId);
		}

		return this.batchInsert(this.paramToT(accessLabelParams, CustomerAccessLabel.class));
	}

	protected CustomerAccessLabel[] getUpdateData(int size) {
		return new CustomerAccessLabel[size];
	}

	@Override
	public <S extends ICustomerAccessLabelUpdateParam> int updateToDatabaseByTranstion(List<S> recive, int customerId) {

		CustomerAccessLabelMapper modulePicMapper = this.getBaseMapper();

		List<CustomerAccessLabel> modulePics = modulePicMapper.getDataByAccessId(customerId);

		Map<Integer, S> map = ListConvertMap.listToMap(recive, ICustomerAccessLabelUpdateParam::getId);

		List<CustomerAccessLabel> updateList = new ArrayList<>();

		List<Integer> delList = new ArrayList<>();

		CustomerAccessLabel toUpdate = null;

		int data;

		ICustomerAccessLabelUpdateParam obtainedFromTheMap;

		Integer id;

		for (CustomerAccessLabel t : modulePics) {

			id = t.getId();

			obtainedFromTheMap = map.get(id);

			if (null != obtainedFromTheMap) { // 更新的
				// 在现有的图片里面可以找到

				data = obtainedFromTheMap.getCustomerId();
				// 比较 data名称是否一致
				if (data == t.getCustomerId()) {
					continue;
				}
				
				toUpdate = new CustomerAccessLabel();
				
				toEntity(toUpdate, data, obtainedFromTheMap);
				
				updateList.add(toUpdate);
				continue;
			}

			// 删除（数据库）
			delList.add(id);

			// 删除(OSS)的
//			delThumbs.add(t.getPic());
		}

		// 要添加的

		List<CustomerAccessLabel> addList = new ArrayList<>();

		CustomerAccessLabel toAdd;
		
		for (S t : recive) {

			if (0 != t.getId().compareTo(0)) {
				continue;
			}
			
			toAdd = new CustomerAccessLabel();

			
			toEntity(toAdd, t.getLabelId(), t);
			
			toAdd.setCustomerId(customerId);

			addList.add(toAdd);
		}

		if (0 != updateList.size()) {

			this.updateBatch(updateList, customerId);
		}

		if (0 != addList.size()) {
			this.batchInsert(addList);
		}

		if (0 != delList.size()) {
			this.delete(delList);
		}

		return 1;

	}

	protected void toEntity(CustomerAccessLabel entity , int labeld, ICustomerAccessLabelUpdateParam obtainedFromTheMap) {
		
		entity.setLabelId(labeld);

		entity.setLabelName(obtainedFromTheMap.getLabelName());

		entity.setLabelPId(obtainedFromTheMap.getLabelPId());
		entity.setLabelParentName(obtainedFromTheMap.getLabelParentName());
		
	}

	@Override
	public <VO extends ICustomerFlagVO> List<VO> getFlag(int customerId, Class<VO> target) {
		
		List<CustomerAccessLabel> accessLabels = this.accessLabelsToCache(customerId);
		
		VO vo;
		
		List<VO> list = new ArrayList<>();
		
		try {
			
			for (CustomerAccessLabel customerAccessLabel : accessLabels) {
				vo = target.newInstance();
				
				vo.setFlagParentName(customerAccessLabel.getLabelParentName());
				
				vo.setFlagChildName(customerAccessLabel.getLabelName());
				
				list.add(vo);
			}
			
			return list;
			
		} catch (Exception e) {
			return list;
		}
	}

	protected List<CustomerAccessLabel> accessLabelsToCache(int accessId) {
		
		String key = RedisConstant.getKeyByCustomerAccessID(accessId);
		
		ValueOperations<String, List<CustomerAccessLabel>> valueOperations = this.redisTemplate.opsForValue();
		
		List<CustomerAccessLabel> accessLabels = valueOperations.get(key);
		
		if (null != accessLabels) {
			return accessLabels;
		}
		
		CustomerAccessLabelMapper accessLabelMapper = this.getBaseMapper();
		
		accessLabels = accessLabelMapper.getDataByAccessId(accessId);
		
		if (null == accessLabels) {
			accessLabels = new ArrayList<>();
			
			valueOperations.set(key, accessLabels, 3, TimeUnit.SECONDS);
			return accessLabels;
		}
		
		valueOperations.set(key, accessLabels, 6, TimeUnit.SECONDS);

		return accessLabels;
	}
	
	
	private RedisTemplate<String, List<CustomerAccessLabel>> redisTemplate;

	@Autowired
	public void setRedisTemplate(RedisTemplate<String, List<CustomerAccessLabel>> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}
}